# Who Cares if You Vote? Partisan agreement and injunctive norms of voting
# Edward Fieldhouse (1), David Cutts (2), Jack Bailey (1)
# (1) The University of Manchester, (2) The University of Birmingham


# 1. Housekeeping ---------------------------------------------------------

# The following code requires that you have first opened the associated
# project file. If not, click the button on the top right and open it
# ("Open Project...").

# Note also that the Bayesian methods we use below rely on having installed
# the probabilistic programming language Stan. If you have yet to install
# Stan, please see https://mc-stan.org/users/interfaces/.

# Load packages

library(tidyverse)
library(ggraph)
library(ggpubr)
library(ggdag)
library(jbmisc)   # devtools::install_github("jackobailey/jbmisc)
library(here)



# 2. Create DAG triptych plot ---------------------------------------------

# We're going to create three DAGs that describe our theory.
# First, we'll produce the raw data for each plot. The first
# highlights the relationship between turnout and normative
# expectations.

dag1 <- 
  dagify(
    V ~ NE,
    coords = 
      list(
        x = c(NE = 0, V = 1),
        y = c(NE = 0, V = 0)
      ),
    labels =
      c(
        V = "Turnout",
        NE = "Normative\nExpectations"
      )
  )


# The second introduces civic duty, which mediates the effect of
# normative expectations on turnout.

dag2 <- 
  dagify(
    V ~ NE + D,
    D ~ NE,
    coords = 
      list(
        x = c(NE = 0, D =  0.5, V = 1),
        y = c(NE = 0, D = -0.5, V = 0)
      ),
    labels =
      c(
        V = "Turnout",
        D = "Civic Duty",
        NE = "Normative\nExpectations"
      )
  )


# The third then introduces dyadic party identification, which
# we expect to moderate both paths from normative expectations.

dag3 <- 
  dagify(
    NE ~ DP,
    X ~ DP,
    Y ~ DP,
    V ~ NE + D,
    D ~ NE,
    coords = 
      list(
        x = c(NE = 0, D =  0.5, V = 1, DP = 0.5, X = 0.5, Y = 0.2),
        y = c(NE = 0, D = -0.5, V = 0, DP = 0.5, X = -0.075, Y = -0.3)
      ),
    labels =
      c(
        V = "Turnout",
        D = "Civic Duty",
        NE = "Normative\nExpectations",
        DP = "Dyadic Party\nIdentificaton",
        X = "",
        Y = ""
      )
  )


# Now that we have the three plots, we can use ggdag to make
# them look nicer. We'll do them one at a time.

dag1 <-
  dag1 %>%
  tidy_dagitty() %>% 
  ggplot(aes(x = x, xend = xend,
             y = y, yend = yend)) +
  geom_dag_point(size = 7,
                 show.legend = F) +
  geom_dag_edges(start_cap = circle(11, "pt"),
                 end_cap = circle(11, "pt"),
                 arrow_directed =
                   grid::arrow(length = grid::unit(4, "pt"),
                               type = "closed"),
                 edge_width = .4,
                 edge_colour = bailey_colours("grey6")) +
  geom_dag_text(family = "Cabin",
                fontface = "bold",
                size = 3) +
  theme_dag() +
  coord_cartesian(xlim = c(0, 1),
                  ylim = c(-0.5, 0.5)) +
  labs(title = "Title",
       subtitle = "Normative expectations affect the\nprobability of turning out to vote.") +
  theme(plot.title = element_blank(),
        plot.subtitle = element_text(family = "Cabin", size = 8, hjust = 0, margin = margin(b = 15, unit = "pt")),
        plot.margin = unit(c(5, 15, 0, 0), "pt"))


dag2 <- 
  dag2 %>%
  tidy_dagitty() %>% 
  ggplot(aes(x = x, xend = xend,
             y = y, yend = yend)) +
  geom_dag_point(size = 7,
                 show.legend = F) +
  geom_dag_edges(start_cap = circle(11, "pt"),
                 end_cap = circle(11, "pt"),
                 arrow_directed =
                   grid::arrow(length = grid::unit(4, "pt"),
                               type = "closed"),
                 edge_width = .4,
                 edge_colour = bailey_colours("grey6")) +
  geom_dag_text(family = "Cabin",
                fontface = "bold",
                size = 3) +
  theme_dag() +
  coord_cartesian(xlim = c(0, 1),
                  ylim = c(-0.5, 0.5)) +
  labs(title = "Title",
       subtitle = "Though part of this effect is direct, part\nis indirect and mediated by civic duty.") +
  theme(plot.title = element_blank(),
        plot.subtitle = element_text(family = "Cabin", size = 8, hjust = 0, margin = margin(b = 15, unit = "pt")),
        plot.margin = unit(c(5, 15, 0, 0), "pt"))


dag3 <-
  dag3 %>%
  tidy_dagitty() %>% 
  mutate(colour = c(rep("a", 7), "b", "b")) %>% 
  ggplot(aes(x = x, xend = xend,
             y = y, yend = yend)) +
  geom_dag_point(aes(colour = colour),
                 size = 7,
                 show.legend = F) +
  geom_dag_edges(start_cap = circle(11, "pt"),
                 end_cap = circle(11, "pt"),
                 arrow_directed =
                   grid::arrow(length = grid::unit(4, "pt"),
                               type = "closed"),
                 edge_width = .4,
                 edge_colour = bailey_colours("grey6")) +
  geom_dag_text(family = "Cabin",
                fontface = "bold",
                size = 3) +
  scale_colour_manual(values = c("black", "white")) +
  theme_dag() +
  coord_cartesian(xlim = c(0, 1),
                  ylim = c(-0.5, 0.5)) +
  labs(title = "Title",
       subtitle = "Dyadic partisanship then moderates the\ndownstream influence of these effects.") +
  theme(plot.title = element_blank(),
        plot.subtitle = element_text(family = "Cabin", size = 8, hjust = 0, margin = margin(b = 15, unit = "pt")),
        plot.margin = unit(c(5, 15, 0, 0), "pt"))


# Next, we'll bind these together into a single plot using
# the ggarrange() function in the ggpubr package.

fig <- ggarrange(dag1, dag2, dag3, nrow = 1)


# And, finally, we'll export this plot as a jpeg.

jpeg(file = here("_output", "paths.jpeg"),
     res = 300,
     width = 5.97,
     height = 2.2,
     units = "in")
fig
dev.off()
